// pages/advance/advance.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeTitle:"请选择项目类型",
    typeKey:-1,
    type:[
        {id:1,name:"审批"},
        {id:2,name:"备案、核准"}
    ],
    statusTitle:"请选择",
    statusKey:-1,
    status:[
      {value:1,title:"已完成"},
      {value:2,title:"未完成"}
    ],
    id:0,
    month:0,
    procedure:0,
    year:0,
    detail:{},
    content:'',
    images: [],
    show_time:'',
    declare_content:""
  },


  onChoosePic(){
    let that = this;
    wx.chooseImage({
    count: 1,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
        url: `${app.globalData.rootUrl}/api/upload/upload`,
        filePath: tempFilePaths[0],
        name: 'file',
        formData: {'appid': app.appid},
        success: res => {
            let response = JSON.parse(res.data);
            if(res.statusCode == 200){
            if(response.code == 0){
                let images = that.data.images;
                images.push(response.thumb);
                that.setData({ images });
            }else{
                app.showFailToast(response.msg);
            }
            }else app.showFailToast('网络错误，请稍后再试');
        }
        });
    }
    });
},

  inputArea:function(e){
    var field = e.currentTarget.dataset.field;
    if(field=="declare_content"){
      this.setData({declare_content:e.detail.value});
    }
  },

  // 选择项目类型
  changeDate:function(e){

    if(e.currentTarget.dataset.key=="status"){
      var status = this.data.status;
      var statusTitle = status[e.detail.value].title;
      this.setData({
        statusTitle:statusTitle,
        statusKey:e.detail.value
      });
    }

    if(e.currentTarget.dataset.key=="type"){
      var type = this.data.type;
      var typeTitle = type[e.detail.value].name;
      this.setData({
        typeTitle:typeTitle,
        typeKey:e.detail.value
      });
    }
   
  },

  /** 取消 */
  onCancel(){
      wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,month:options.month,procedure:options.procedure,year:options.year
    });
    this.addProcedureApi();
  },

  //提交操作
  onSumit:function(){
      /*var statusKey = this.data.statusKey;
      if(statusKey==-1){
        app.showFailToast("请选择完成情况");
        return;
      }
      var status = this.data.status;
      var status = status[statusKey].value;*/


      let params = {
        pro_id: this.data.id,
        year: this.data.year,
        month:this.data.month,
        type_id:this.data.procedure,
        declare_content:this.data.declare_content,
        declare_image:this.data.images,
        status:1
      };

      var type = this.data.type;
      if(type.length!=0){
        var typeKey = this.data.typeKey;
        if(typeKey==-1){
          app.showFailToast("请选择项目类型");return;
        }
        var sub_id = type[typeKey].id;
        params.sub_id = sub_id;
      }

      app.request('post', '/api/Project/advanceDo', params).then(res => {
          app.showFailToast(res.data.msg);
          if(res.data.code==0){
            wx.navigateBack();
          }
      }).catch(err => console.log(err));


  },

  // 查询推进详情
  addProcedureApi:function(){
      let params = {
        id: this.data.id,
        year: this.data.year,
        month:this.data.month,
        procedure:this.data.procedure
      };
      app.request('post', '/api/Project/addProcedureApi', params).then(res => {

        var detail = res.data.detail;
          
        //完成情况赋值
        this.setData({content:detail.content});
        var status = this.data.status;
        var statusKey = detail.status;
        if(statusKey!=''){
          statusKey = statusKey-1;
          var statusTitle = status[statusKey].title;
          this.setData({
            statusTitle:statusTitle,
            statusKey:statusKey
          });
        }
        //完成情况赋值end

        //项目类型赋值
        var project_type = res.data.project_type;
        var sub_id = detail.sub_id;
        console.log(sub_id);
        if(sub_id!=''){
          for (var i = 0; i<project_type.length; ++i) {  
            if(project_type[i].id == sub_id){
              var typeKey = i;
              var typeTitle = project_type[i].name;
              this.setData({
                typeKey:typeKey,
                typeTitle:typeTitle
              });
              break;
            }
          }
        }
        //项目类型赋值end


        this.setData({
          type:project_type,
          detail:detail,
          images:res.data.images,
          declare_content:detail.declare_content
        });
          
      }).catch(err => console.log(err));
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var date = this.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
        show_time:date
    });
  },

  formatTime:function(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return year + "-" + month + "-" + day;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  previewImg1: function (e) {
    let that = this;
    // var figureList = e.currentTarget.dataset.v;
    let figureList = that.data.images;
    wx.previewImage({
      current: e.currentTarget.dataset.src,   //当前图片地址
      urls: figureList,        //所有要预览的图片的地址集合 数组形式
    });
  },
})